package com.trt.workflow;

import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * @author 谭仁涛
 * @description 任务处理
 * @date 2022/2/19 18:44
 */
@SpringBootTest
public class Part4_Task {

    @Autowired
    private TaskService taskService;

    // 任务查询
    @Test
    public void getTasks() {
        List<Task> list = taskService.createTaskQuery().list();
        for (Task tk : list) {
            System.out.println("Id :" + tk.getId());
            System.out.println("Name : " + tk.getName());
            System.out.println("Assignee :" + tk.getAssignee());
        }
    }

    // 查询我的代办任务
    @Test
    public void getTasksByAssignee() {
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee("wukong")
                // .taskCandidateUser() //需要集成安全框架机制
                .list();
        for (Task tk : list) {
            System.out.println("Id :" + tk.getId());
            System.out.println("Name : " + tk.getName());
            System.out.println("Assignee :" + tk.getAssignee());
        }
    }

    // 执行任务
    @Test
    public void completeTask() {
        // taskService.complete("935ba275-9175-11ec-9fdf-00ff6fa8c6c1");
        taskService.complete("7315a724-9176-11ec-a41a-00ff6fa8c6c1");
        System.out.println("完成任务");
    }


    // 拾取任务
    @Test
    public void claimTask() {
        Task task = taskService.createTaskQuery().taskId("2b68e6f3-917c-11ec-8ed0-00ff6fa8c6c1").singleResult();
        taskService.claim("2b68e6f3-917c-11ec-8ed0-00ff6fa8c6c1", "bajie"); // 参数1：taskId, 参数2：userId
    }

    //归还与交办任务
    @Test
    public void setTaskAssignee(){
        Task task = taskService.createTaskQuery().taskId("2b68e6f3-917c-11ec-8ed0-00ff6fa8c6c1").singleResult();
        taskService.setAssignee("2b68e6f3-917c-11ec-8ed0-00ff6fa8c6c1","null");//归还候选任务  // 参数1：taskId, 参数2：userId
        taskService.setAssignee("2b68e6f3-917c-11ec-8ed0-00ff6fa8c6c1","wukong");//交办
    }
}
